
args param_def param_ind param_sales param_lperc
// global lvmkt should be defined in the main script

// List of percentiles
global param_lperc = "`param_lperc'"
if "`param_lperc'" == "" {
	global param_lperc = "10 20 30 40 50 60 70 80 90 100"
}

//==============================================================================
// Function that select years if we are analyzing sales
// Using data corresponding to the years of sales data

capture program drop f_sales_year
program f_sales_year
	merge m:1 ch_ind using ${dir_data}/cw_year_sales, gen(m_year)
	tab ch_ind if m_year != 3
	keep if m_year == 3
	drop m_year
	
	replace year_max = 2013 // Force to use 2013 for ending year
	keep if year == year_min | year == year_max
	
	rename year year_orig_mkt
	gen year = .
	replace year = ${year1} if year_orig == year_min
	replace year = ${year2} if year_orig == year_max
end

//==============================================================================

/* 
Conventions for variables related to percentiles

_##: Top ##%
_##d: Top (##-10)-##%
_##b: Bottom (100-##)%
*/

//------------------------------------------------------------------------------
// If we define top firms based on employment
if "`param_def'" == "emp" {

use ${ds_ind}, clear
gen ind_est_tot = est_ind
gen est_ind_100 = est_ind
gen est_ind_0 = 0
foreach iperc in 0 1 $param_lperc {
	capture noi gen ind_est_tot_`iperc' = est_ind_`iperc'
	if `iperc' >= 10 {
	// Deciles
	capture noi gen ind_est_tot_`iperc'd = ind_est_tot_`iperc' - ind_est_tot_`=`iperc'-10'
	}
}
gen ind_est_tot_10b = est_ind - est_ind_10

if "`param_sales'"=="sales" f_sales_year

tab year if _merge != 3
keep if inlist(year,${year1},${year2})
tab year if _merge != 3

keep year sector ch_ind ind_*_tot n_ind ind_*_tot_* n_ind_*
}

//------------------------------------------------------------------------------
// If we define top firms based on number of markets
else if "`param_def'" == "geo" {

if "`param_ind'" == "bea" {
	use ${dir_data}/mkt_ind_bea_ip.dta, clear
	gen sector = .
}
else if "`param_ind'" == "bea2d" {
	use ${dir_data}/mkt_ind_bea_ip_2d.dta, clear
	gen sector = .
}
else {
	use ${dir_data}/mkt_ind_sum, clear
	merge 1:1 year ch_ind using ${ds_ind}, keep(match master) keepus(sector emp_ind)
}

// Rename to make it consistent with emp-based names
foreach vmkt in $lvmkt {
	gen ind_`vmkt'_tot = n_`vmkt'
	gen n_`vmkt'_100 = n_`vmkt'
	gen n_`vmkt'_0 = 0
	
	foreach iperc in 0 1 $param_lperc {
		capture noi gen ind_`vmkt'_tot_`iperc' = n_`vmkt'_`iperc'
		if `iperc' >= 10 {
		// Deciles
		capture noi gen ind_`vmkt'_tot_`iperc'd = ind_`vmkt'_tot_`iperc' - ind_`vmkt'_tot_`=`iperc'-10'
		}
	}
    gen ind_`vmkt'_tot_10b = n_`vmkt' - n_`vmkt'_10
}
gen n_ind = n_firm
gen n_firm_100 = n_firm
gen n_firm_0 = 0
foreach iperc in 0 1 $param_lperc {
	capture noi gen n_ind_`iperc' = n_firm_`iperc'
	if `iperc' >= 10 {
	// Deciles
	capture noi gen n_ind_`iperc'd = n_ind_`iperc' - n_ind_`=`iperc'-10'
	}
}
gen n_ind_10b = n_firm - n_firm_10

if "`param_sales'"=="sales" f_sales_year
keep if inlist(year,${year1},${year2})

keep year sector ch_ind ind_*_tot n_ind ind_*_tot_* n_ind_* emp_ind
}

//==============================================================================
/*
Function that
1) Calculates markets per firm
2) Multiplies the value in year1 with -1 (in order to use collapse to calculate difference)
*/
capture program drop f_neg
program f_neg
	args vmkt vperc
	
	gen mu_mkt`vperc' = ind_`vmkt'_tot`vperc' / n_ind`vperc'
	gen ln_mu_mkt`vperc' = ln(mu_mkt`vperc')

	replace mu_mkt`vperc' = - mu_mkt`vperc' if year == ${year1}
	replace ln_mu_mkt`vperc' = - ln_mu_mkt`vperc' if year == ${year1}

	rename mu_mkt`vperc' mu_mkt_`vmkt'_d`vperc'
	rename ln_mu_mkt`vperc' ln_mu_mkt_`vmkt'_d`vperc'
	
end

foreach vmkt in $lvmkt {
	
	f_neg "`vmkt'" ""
	
	f_neg "`vmkt'" "_1"
	foreach iperc in $param_lperc {
		f_neg "`vmkt'" "_`iperc'"
		f_neg "`vmkt'" "_`iperc'd"
	}
    f_neg "`vmkt'" "_10b"

}

//==============================================================================
// Est as market

local vmkt = "est"
gen mu_emp_mkt = emp_ind / ind_`vmkt'_tot
gen ln_mu_emp_mkt = ln(mu_emp_mkt)
replace mu_emp_mkt = - mu_emp_mkt if year == ${year1}
replace ln_mu_emp_mkt = - ln_mu_emp_mkt if year == ${year1}
rename mu_emp_mkt mu_emp_mkt_`vmkt'_d
rename ln_mu_emp_mkt ln_mu_emp_mkt_`vmkt'_d

//==============================================================================
// Collapse to calculate the difference

collapse (sum) mu_mkt_* ln_mu_mkt_* mu_emp* ln_mu_emp* (min) n_ind*, by(sector ch_ind) // n_ind shows min # of firms

//==============================================================================
// Replace values for industry with missing top percentiles with .

capture program drop f_miss
program f_miss
	args vmkt vperc
	
	replace mu_mkt_`vmkt'_d`vperc' = . if n_ind`vperc' == 0
	replace ln_mu_mkt_`vmkt'_d`vperc' = . if n_ind`vperc' == 0
end

foreach vmkt in $lvmkt {
	
	f_miss "`vmkt'" ""
	
	f_miss "`vmkt'" "_1"
	foreach iperc in $param_lperc {
		f_miss "`vmkt'" "_`iperc'"
		f_miss "`vmkt'" "_`iperc'd"
	}
    f_miss "`vmkt'" "_10b"

}

local vmkt = "est"
replace mu_emp_mkt_`vmkt'_d = . if n_ind == 0
replace ln_mu_mkt_`vmkt'_d = . if n_ind == 0

//==============================================================================
// Merge with additional data sets if needed

if ! regexm("`param_ind'","bea") {
// Default weight and selected difference variable
merge 1:1 ch_ind using ${dir_proj}/data/ind_sum_d_sel_10_1977_2013, assert(match using)
tab ch_ind if _merge != 3
drop _merge

*Merge with SV weights generated in sv_weights.do: 
merge 1:1 ch_ind using ${dir_proj}/output/202011_main/sv_weights/sv_weights_1977_2013, assert(match) keepusing(w_num* w_sv*) nogen
rename w_num w_sv_numerator
}
else {
// BEA IP data
tempfile dt_temp
save `dt_temp'_a, replace

if "`param_ind'" == "bea" {
	use ${dir_data}/mkt_ind_bea_ip.dta, clear
	gen sector = .
}
else if "`param_ind'" == "bea2d" {
	use ${dir_data}/mkt_ind_bea_ip_2d.dta, clear
	gen sector = .
}

keep if inlist(year,${year1},${year2})
// Calculate weight: average emp share
sort year ch_ind
by year: egen emp_tot = total(emp_ind)
gen emps_ind = emp_ind / emp_tot
collapse (mean) w_emps=emps_ind, by(ch_ind)
merge 1:1 ch_ind using `dt_temp'_a, assert(match) nogen

}

// End of do file
